package com.example.demo.controller;

import com.example.demo.domain.CategoryDO;
import com.example.demo.domain.HbdItemDictDO;
import com.example.demo.mapper.CategoryMapper;
import com.example.demo.mapper.HbdItemDictMapper;
import com.example.demo.service.QueryCategoryOperation;
import com.example.demo.service.QueryItemDictOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class VisitorController {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private HbdItemDictMapper itemDictMapper;

    @Autowired
    private ApplicationContext applicationContext;

    @GetMapping("/category/root")
    public List<CategoryDO> root(){
        List<CategoryDO> roots = categoryMapper.listRoots();
        for(CategoryDO child:roots){
            QueryCategoryOperation queryCategoryOperation = applicationContext.getBean(QueryCategoryOperation.class);
            child.execute(queryCategoryOperation);
        }
        return roots;
    }

    @GetMapping("/item/root")
    public List<HbdItemDictDO> items(){
        List<HbdItemDictDO> roots = itemDictMapper.listRoots();

        for(HbdItemDictDO child:roots){
            QueryItemDictOperation queryItemDictOperation = applicationContext.getBean(QueryItemDictOperation.class);
            child.opeate(queryItemDictOperation);
        }
        return roots;
    }

}
